// myCourse_package/pages/courseOrder/courseOrder.ts
import {getSeriesDetail} from '../../../api/recommedCourse'
import {getMemberList} from '../../../api/member'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false, //显示弹框
    studentList:[],
    studentSelectList:[], //学员选中切换
    // courseSelect:false,//课程选中
    postData:{
    },
    orderCourseInfo:{

    } //课程详情
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(data:any) {
    const {serialId} = data
    this.setData({
      postData:{
        serialId
      }
    })
    this.init()
    this.getOrderInfo()
    this.getStudentList()
  },
  init(){

  },
  // 获取订单课程信息
  async getOrderInfo(){
    const postData = this.data.postData as any
   const {code,data:res} = await getSeriesDetail(postData)
   if(code === 200){
     this.setData({
      orderCourseInfo:res
     })
   }
  },
      // 获取学员列表
      async getStudentList(){
        // const postData = this.data.postData as any
        // 获取用户id
        const userId = getApp().getUserInfo().userId
        const {code,data:res} = await getMemberList({
          userId
        })
        const studentSelectList = res.map(()=>{
          return false
        })
        if(code === 200){
          this.setData({
            studentList:res,
            studentSelectList
          })
        }
       },
  // 选择学生
  checkStudent(e:any){
    const {index} = e.currentTarget.dataset    
    const list = this.data.studentSelectList as any
    list.forEach((val:boolean,ind:number)=>{
      if(index !== ind){
        list[ind] = false

      }
    })
    list[index] = !list[index]
    this.setData({
      studentSelectList:list
    })
  },
// 提交订单
comfirmOrder(){
  const flagBool = this.data.studentSelectList.some(val=>val === true)
 if(!flagBool){
   return wx.Utils.toast("请选择学员")
 }else{
 const {serialId} = this.data.postData as any
let index =  this.data.studentSelectList.findIndex(val=>val === true)
  const purser = this.data.studentList[index]
  const {stuId,userName} = purser
  wx.navigateTo({
    url:`/myCourse_package/pages/confirmOrders/confirmOrders?stuId=${stuId}&userName=${userName}&serialId=${serialId}&type=confirm`
  })
 }
},

// 跳转添加学员
toAddMember(){
    wx.navigateTo({
      url:"/curriculum_package/pages/addStudents/addStudents"
    })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})